package com.ll.sender;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.ll.config.EmailServerConfiguration;
import com.ll.service.EmailMessageService;
import com.ll.service.EmailServerService;
import com.ll.vo.response.EmailMessageResponseVo;
import com.ll.vo.response.EmailServerResponseVo;

/**
 * 定时任务
 * 
 * @author liudawei
 * @date 2016年2月2日下午6:15:51
 */
@Service
public class EmailSenderFailover {


	@Autowired
	private EmailMessageService emailMessageService;
	@Autowired
	private EmailServerService emailServerService;

	private EmailServerConfiguration emailServerConfiguration = new EmailServerConfiguration();

	// private static long scheduledTime;
	//
	// static {
	// scheduledTime =
	// emailServerConfiguration.getScheduledTime("scheduledTime");
	// }

	public EmailSenderFailover() {
	}

	@Scheduled(fixedDelay = 5000)
	public void scheduledTask() {
		//
		String uuid = emailMessageService.lockEmailMessage(emailServerConfiguration.getSize("size"));
		//
		List<EmailMessageResponseVo> list = emailMessageService.findByLocked(uuid);
		// 查询邮件服务
		if (list != null) {
			EmailServerResponseVo emailServerResponseVo = emailServerService
					.findByShopId(emailServerConfiguration.getShopId("shopId"));
			for (EmailMessageResponseVo item : list) {
				item.setEmailServerResponseVo(emailServerResponseVo);
				TaskQueue.addQueue(item);
			}
		}
	}
	@Scheduled(fixedDelay = 10000)
	public void unLockscheduledTask() {
		emailMessageService.unlockEmailMessageBylockTime(emailServerConfiguration.getScheduledTime("lockTime"));
	}
}
